//
//  teamRankViewController.m
//  pe_score
//
//  Created by zwz on 16/6/26.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "teamRankViewController.h"
#import "rankTableViewCell.h"
#import "teamViewController.h"
#import "teamRankModel.h"
#import "addTeamViewController.h"

@interface teamRankViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_TableView;
    UIImageView *_imageView;
}
@property(nonatomic,copy)NSArray * mydataarray;

@end

@implementation teamRankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatTableView];
    [self creatView];
    [self getData];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)creatTableView
{
    _TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-114)];
    _TableView.dataSource = self;
    _TableView.delegate = self;
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@""]]];
    
    _TableView.backgroundColor = [UIColor clearColor];
    [_TableView setBackgroundView:_imageView];
    [self.view addSubview:_TableView];
}

-(void)creatView
{
    UIBarButtonItem * bar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(clickNavBtn:)];
    bar.title = @"增加队伍";
    self.navigationItem.rightBarButtonItem=bar;
}


-(void)clickNavBtn:(UIBarButtonItem *)btn
{
    addTeamViewController *addTeam = [[addTeamViewController alloc] init];
    [self.navigationController pushViewController:addTeam animated:YES];
}


-(void)getData
{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 30.0f;
    config.timeoutIntervalForResource = 60.0f;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"http://23.105.222.161/abc/run_query.php?type=2"]completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSArray *res = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSMutableArray *newMy = [[NSMutableArray alloc]init];
        for (NSDictionary *x in res) {
            teamRankModel *newModel = [teamRankModel new];
            newModel.teamName = [x valueForKey:@"team_name"];
            
            [newMy addObject:newModel];
        }
        _mydataarray = newMy;
        
        [_TableView reloadData];
    }];
    [task resume];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_mydataarray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"rankTableViewCell";
    rankTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.backgroundColor = nil;
    teamRankModel *model=_mydataarray[indexPath.row];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellID owner:self options:nil] lastObject];
    }
    
    cell.rank.text=[NSString stringWithFormat:@"%d",indexPath.row+1];
    cell.name.text=model.teamName;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    rankTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    teamViewController *team=[[teamViewController alloc] init];
    team.teamName=cell.name;
    [self.navigationController pushViewController:team animated:YES];
    
}


@end
